<?php
  /**
  * Copyright 2011 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may
  * not use this file except in compliance with the License. You may obtain
  * a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */

  require 'includes/facebook.php';

  define('FACEBOOK_APP_ID',"160596754040030");
  define('FACEBOOK_SECRET',"20ea9c13cac64e709f346cac489669f9");

  $user = null;

  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
  'appId' => FACEBOOK_APP_ID,
  'secret' => FACEBOOK_SECRET,
  'cookie' => true
  ));

  // Get User ID
  $user = $facebook->getUser();

  // We may or may not have this data based on whether the user is logged in.
  //
  // If we have a $user id here, it means we know the user is logged into
  // Facebook, but we don't know if the access token is valid. An access
  // token is invalid if the user logged out of Facebook.

  if ($user) {
    try {
      // Proceed knowing you have a logged in user who's authenticated.
      $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
      error_log($e);
      $user = null;
    }
  }   

  // Login or logout url will be needed depending on current user state.
  if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();

  } else {
    $params = array('scope' => 'email, publish_actions, publish_stream, read_stream, offline_access');
    $loginUrl = $facebook->getLoginUrl($params);
    echo("<script> top.location.href='" . $loginUrl . "'</script>");
    exit();
  }

  var_dump($user_profile); die;  

  // This call will always work since we are fetching public data.
  $MittoTran = $facebook->api('/MittoTran'); 
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title> 
    <link type="text/css" rel="stylesheet" href="http://static.ak.fbcdn.net/rsrc.php/v1/y-/r/lP-KiSMrQa8.css" />
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
      <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
      <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
      <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
      <?php endif ?>

    <h3>User Email</h3>
    <p>
      <?php echo $user_profile['email']; ?>
    </p>

    <h3>Public profile of MittoTran</h3>
    <img src="https://graph.facebook.com/MittoTran/picture">
    <?php echo $MittoTran['name']; ?>
  </body>
  </html>  